package de.ifgi;

import java.util.ArrayList;
import java.util.List;

import android.util.Log;

public class SignalCircles {

	private double x1;
	private double y1;
	private double r1;
	private double x2;
	private double y2;
	private double r2;
	
	private double d;
	
	public SignalCircles(Point p1, double r1, Point p2, double r2) {
		x1 = p1.getX();
		y1 = p1.getY();
		x2 = p2.getX();
		y2 = p2.getY();
		
		d = p1.distance(p2);
		
		this.r1 = r1;
		this.r2 = r2;
	}
	
	public SignalCircles(Node n1, Node n2) {
		Point p1 = n1.getEstPosition();
		Point p2 = n2.getEstPosition();
		
		x1 = p1.getX();
		y1 = p1.getY();
		x2 = p2.getX();
		y2 = p2.getY();	
		
		// distance between the nodes
		d = p1.distance(p2);
		
		// circles radii
		r1 = n1.getEstDistance();
		r2 = n2.getEstDistance();
	}
	
	/**
	 * If d < r1 + r2 then the circles intersect each other. 
	 * If d = r1 + r2 then the circles touch each other. 
	 * If d > r1 + r2 then there are no solutions because the circles are separate. 
	 * If d < |r1 - r2| then there are no solutions because one circle is contained within the other. 
	 * If d = 0 and r1 = r2 then the circles are coincident and there are an infinite number of solutions.
	 * @return true or false
	 */
	public boolean intersect() {
		boolean intersect = false;
//		if (d < r1 + r2 || d == r1 + r2)
		if (d < r1 + r2)
			intersect = true;
		else if (d > r1 + r2)
			intersect = false;
		else if (d < Math.abs(r1 - r2))
			intersect = false;
		else if (d == 0 && r1 == r2)
			intersect = false;
		return intersect;
	}
    
    public List<Point> getIntersectionPoints() {
    	List<Point> intersectionPts = new ArrayList<Point>();
//		Log.d("OIL", "d="+d+", r1="+r1+", "+"r2="+r2);
    	double a = (Math.pow(r1, 2) - Math.pow(r2, 2) + Math.pow(d, 2))/(2*d);
//    	Log.d("OIL", "a="+a);
    	double dx = x2 - x1;
//    	Log.d("OIL", "dx="+dx);
    	double dy = y2 - y1;
//    	Log.d("OIL", "dy"+dy);
    	double tx = x1 + (dx*a / d);
//    	Log.d("OIL", "tx="+tx);
    	double ty = y1 + (dy*a / d);
//    	Log.d("OIL", "ty="+ty);
    	// r1 will always be greater than a
    	double h = Math.sqrt(Math.pow(r1, 2) - Math.pow(a, 2));
//    	Log.d("OIL", "h="+h);
    	double xi = tx - (dy*h / d);
//    	Log.d("OIL", "xi="+xi);
    	double xi_prime = tx + (dy*h / d);
//    	Log.d("OIL", "xi_prime="+xi);
    	double yi = ty + (dx*h / d);
//    	Log.d("OIL", "yi="+yi);
    	double yi_prime = ty - (dx*h / d);
//    	Log.d("OIL", "yi_prime="+yi_prime);
//    	Log.d("OIL", "xi, yi="+xi+","+yi);
//    	Log.d("OIL", "xi_prime, yi_prime="+xi_prime+","+yi_prime);
    	
    	if (!Double.isNaN(xi) && !Double.isNaN(yi) && !Double.isNaN(xi_prime) && !Double.isNaN(yi_prime)) {
        	Point pi = new Point(xi, yi);
        	Point pi_prime = new Point(xi_prime, yi_prime);
        	intersectionPts.add(pi);
        	intersectionPts.add(pi_prime);
		} else {
			Log.d("OIL", "NaN");
		}
		
    	return intersectionPts;
    }
    
}
